package cn.iocoder.yudao.module.trade.service.depositproductrelation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.CollectionUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.controller.admin.depositorderreturn.vo.DepositOrderReturnSaveReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.depositproductrelation.vo.DepositProductRelationPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.depositproductrelation.vo.DepositProductRelationSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderreturnlog.DepositOrderReturnLogDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositproductrelation.DepositProductRelationDO;
import cn.iocoder.yudao.module.trade.dal.mysql.depositproductrelation.DepositProductRelationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.DEPOSIT_PRODUCT_RELATION_NOT_EXISTS;

/**
 * 钢瓶与押金关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DepositProductRelationServiceImpl implements DepositProductRelationService {

    @Resource
    private DepositProductRelationMapper depositProductRelationMapper;

    @Override
    public Long createDepositProductRelation(DepositProductRelationSaveReqVO createReqVO) {
        // 插入
        DepositProductRelationDO depositProductRelation = BeanUtils.toBean(createReqVO, DepositProductRelationDO.class);
        depositProductRelationMapper.insert(depositProductRelation);
        // 返回
        return depositProductRelation.getId();
    }

    @Override
    public void updateDepositProductRelation(DepositProductRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateDepositProductRelationExists(updateReqVO.getId());
        // 更新
        DepositProductRelationDO updateObj = BeanUtils.toBean(updateReqVO, DepositProductRelationDO.class);
        depositProductRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteDepositProductRelation(Long id) {
        // 校验存在
        validateDepositProductRelationExists(id);
        // 删除
        depositProductRelationMapper.deleteById(id);
    }

    private void validateDepositProductRelationExists(Long id) {
        if (depositProductRelationMapper.selectById(id) == null) {
            throw exception(DEPOSIT_PRODUCT_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public DepositProductRelationDO getDepositProductRelation(Long id) {
        return depositProductRelationMapper.selectById(id);
    }

    @Override
    public List<DepositProductRelationDO> getDepositProductRelationList(Long depositId) {
        LambdaQueryWrapperX<DepositProductRelationDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(DepositProductRelationDO::getDepositId,depositId);
        return depositProductRelationMapper.selectList(queryWrapperX);
    }

    @Override
    public PageResult<DepositProductRelationDO> getDepositProductRelationPage(DepositProductRelationPageReqVO pageReqVO) {
        return depositProductRelationMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDepositProductRelationBySkuId(List<DepositOrderReturnLogDO> voList, Long depositId) {
        if (!CollectionUtil.isEmpty(voList)) {
            voList.forEach(x -> {
                LambdaQueryWrapperX<DepositProductRelationDO> queryWrapperX = new LambdaQueryWrapperX<>();
                queryWrapperX.eq(DepositProductRelationDO::getSkuId, x.getSkuId());
                queryWrapperX.eq(DepositProductRelationDO::getDepositId, depositId);
                DepositProductRelationDO relationDO = depositProductRelationMapper.selectOne(queryWrapperX);
                // 设置退瓶数量
                relationDO.setBottleReturnCount(x.getBottleReturnCount());
                if (Objects.equals(relationDO.getSkuCount(), x.getBottleReturnCount())) {
                    // 设置为已退瓶
                    relationDO.setStatus(1);
                }
                depositProductRelationMapper.updateById(relationDO);
            });
        }
    }

}
